﻿using HKB.Common;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Text;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class Admin_Admin_AddAdminGroup : Admin, IRequiresSessionState
{
    private AdminGroupBll _adminGroup = null;
    private AdminBll _admin = null;
    private int adminGroupId;
    protected AdminGroupBll m_adminGroup
    {
        get
        {
            AdminGroupBll adminGroup;
            if (this._adminGroup == null)
            {
                this._adminGroup = new AdminGroupBll(this.conn);
                adminGroup = this._adminGroup;
            }
            else
            {
                adminGroup = this._adminGroup;
            }
            return adminGroup;
        }
    }
    protected AdminBll m_admin
    {
        get
        {
            AdminBll admin;
            if (this._admin == null)
            {
                this._admin = new AdminBll(this.conn);
                admin = this._admin;
            }
            else
            {
                admin = this._admin;
            }
            return admin;
        }
    }
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.adminGroupId = Fetch.GetID("AdminGroupID");
        if (!base.admin.CheckSuperAdmin())
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", this.adminGroupId.ToString()), "AdminGroup");
            base.WriteRightErrMsg();
        }
        if (!base.IsPostBack)
        {
            DataTable allAdmin = this.m_admin.GetAllAdmin();
            this.ListUser.DataSource = allAdmin;
            this.ListUser.DataTextField = "UserName";
            this.ListUser.DataValueField = "Admin_ID";
            this.ListUser.DataBind();
            if (this.adminGroupId != 0)
            {
                this.BindBaseDate(this.adminGroupId);
                this.ButUpdate.Visible = true;
                this.ButAdd.Visible = false;
            }
            this.CheckAccess.Attributes.Add("onclick", "return ShowRowPower()");
        }
    }
    protected void BindBaseDate(int AdminGroupID)
    {
        DataTable adminGroupByID = this.m_adminGroup.GetAdminGroupByID(AdminGroupID);
        if (adminGroupByID.Rows.Count > 0)
        {
            DataTable adminByGroupID = this.m_admin.GetAdminByGroupID(AdminGroupID);
            for (int i = 0; i < adminByGroupID.Rows.Count; i++)
            {
                foreach (ListItem listItem in this.ListUser.Items)
                {
                    if (listItem.Value.Trim() == adminByGroupID.Rows[i]["Admin_ID"].ToString().Trim())
                    {
                        listItem.Selected = true;
                    }
                }
            }
            string power = adminGroupByID.Rows[0]["Power"].ToString();
            if (adminGroupByID.Rows[0]["Access"].ToString() == "1")
            {
                this.CheckAccess.Checked = true;
            }
            else
            {
                this.CheckAccess.Checked = false;
                this.rowpower.Visible = true;
            }
            this.TxtRemark.Text = adminGroupByID.Rows[0]["Remark"].ToString();
            this.TxtGroupName.Text = adminGroupByID.Rows[0]["AdminGroupName"].ToString();
            this.SetPower(power);
        }
    }
    protected void Button_Command(object sender, CommandEventArgs e)
    {
        int iD = Fetch.GetID("AdminGroupID");
        string power = this.GetPower();
        string text = "";
        foreach (ListItem listItem in this.ListUser.Items)
        {
            if (listItem.Selected)
            {
                text = text + listItem.Value + ",";
            }
        }
        if (text.Length > 1)
        {
            text = text.Remove(text.Length - 1);
        }
        int access = 0;
        if (this.CheckAccess.Checked)
        {
            access = 1;
        }
        if (e.CommandName == "add")
        {
            if (this.m_adminGroup.AddAdminGroup(this.TxtGroupName.Text, access, this.TxtRemark.Text, power, text))
            {
                base.WriteLog("AddNew", Language.GetAdminMsg("Log/ObjectID", "0"), "AdminGroup");
                this.WriteSuccessMsg("", Language.GetAdminMsg("Common/AddSuccess"), "Admin_AdminGroup.aspx");
            }
            else
            {
                this.WriteErrMsg("", Language.GetAdminMsg("Common/AddFail", "未知"), "");
            }
        }
        else
        {
            if (this.m_adminGroup.UpdateGroup(this.TxtGroupName.Text, access, this.TxtRemark.Text, power, iD, text))
            {
                base.WriteLog("Modify", Language.GetAdminMsg("Log/ObjectID", iD.ToString()), "AdminGroup");
                this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_AdminGroup.aspx");
            }
            else
            {
                this.WriteErrMsg("", Language.GetAdminMsg("Common/EditFail", "未知"), "");
            }
        }
    }
    protected string getcb(CheckBox cb)
    {
        string result;
        if (cb.Checked)
        {
            result = "1";
        }
        else
        {
            result = "0";
        }
        return result;
    }
    protected void setcb(CheckBox cb, string val)
    {
        try
        {
            if (val.Trim() == "1")
            {
                cb.Checked = true;
            }
            else
            {
                cb.Checked = false;
            }
        }
        catch
        {
        }
    }
    protected CheckBox[] GetCheckBoxes()
    {
        return new CheckBox[]
		{
			this.cb_MS,
			this.cb_Channel,
			this.cb_announce,
			this.cb_advertisement,
			this.cb_conformity,
			this.cb_CheckUser,
			this.cb_Pass,
			this.cb_Group,
			this.cb_ShortMessage,
			this.cb_Email,
			this.cb_ShopArticle,
			this.cb_Project,
			this.cb_SiteSkin,
			this.cb_Team,
			this.cb_DefindLable,
			this.cb_DeFindPage,
			this.cb_SetiSpecial,
			this.cb_ShopManage,
			this.cb_ShopMessage,
			this.cb_ShopRequest,
			this.cb_ShopProduct,
			this.cb_ShopOrder,
			this.cb_ShopSale,
			this.cb_ShopReview,
			this.cb_Pic,
			this.cb_ShopArea,
			this.cb_ShopCard,
			this.cb_ShopMoney,
			this.cb_SitePayType,
			this.cb_ShopBank,
			this.cb_ShopPayType,
			this.cb_ShopDelive,
			this.cb_ShopVipUser,
			this.cb_ShopVipLevel,
			this.cb_ShopSetup,
			this.cb_MobileMessage,
			this.cb_SubSite,
			this.cb_Witkey,
			this.cb_vipcardmanage,
			this.cb_giftcardmanage,
			this.cb_lotterymanage,
			this.cb_tryclothesmanage,
			this.cb_couponmanage,
			this.cb_financemanage,
			this.cb_useclientrelation,
			this.cb_stockmanage,
			this.cb_jxcmanage
		};
    }
    protected string GetPower()
    {
        StringBuilder stringBuilder = new StringBuilder();
        CheckBox[] checkBoxes = this.GetCheckBoxes();
        int i;
        for (i = 0; i < checkBoxes.Length - 1; i++)
        {
            stringBuilder.Append(this.getcb(checkBoxes[i]) + "|");
        }
        stringBuilder.Append(this.getcb(checkBoxes[i]));
        return stringBuilder.ToString();
    }
    protected void SetPower(string Power)
    {
        string[] array = Power.Split(new char[]
		{
			'|'
		});
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = array[i].Substring(0, 1);
        }
        try
        {
            CheckBox[] checkBoxes = this.GetCheckBoxes();
            for (int i = 0; i < checkBoxes.Length; i++)
            {
                this.setcb(checkBoxes[i], array[i]);
            }
        }
        catch
        {
        }
    }
}
